package com.example.tests;

import static com.example.fw.WebDriverHelperBase.getPrefix;
import static com.example.fw.RandomizeHelper.generateRandomSuffix;
import static com.example.fw.RandomizeHelper.getRandomMonth;
import static com.example.fw.RandomizeHelper.getRandomNumber;
import static com.example.fw.RandomizeHelper.getRandomNumbers;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.XStream;

public class ContactsDataGenerator
{
	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException
	{
		if (args.length < 3)
		{
			System.out.println("Please specift parameters: <amount of test data> <file> <format>");
		}
		int amount = Integer.parseInt(args[0]);
		File file = new File(args[1]);
		String format = args[2];

		if (file.exists())
		{
			System.out.println("File exist, please remove it manually: " + file);
			return;
		}

		List<ContactData> contacts = generateRandomContacts(amount);
		if ("csv".equals(format))
		{
			saveContactsToCsvFile(contacts, file);
		}
		else if ("xml".equals(format))
		{
			saveContactsToXmlFile(contacts, file);
		}
		else
		{
			System.out.println("Unknown format" + format);
		}
	}

	private static void saveContactsToXmlFile(List<ContactData> contacts, File file) throws IOException
	{
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		String xml = xstream.toXML(contacts);

		FileWriter writer = new FileWriter(file);
		writer.write(xml);
		writer.close();
	}

	public static List<ContactData> loadContactsFromXmlFile(File file) throws IOException
	{
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		return (List<ContactData>) xstream.fromXML(file);
	}

	private static void saveContactsToCsvFile(List<ContactData> contacts, File file) throws IOException
	{
		FileWriter writer = new FileWriter(file);
		for (ContactData contact : contacts)
		{
			writer.write(contact.getFirstname() + "," + contact.getLastname() + "," + contact.getAddress() + "," + contact.getPhoneHome() + ","
					+ contact.getPhoneMobile() + "," + contact.getPhoneWork() + "," + contact.getEmail1() + "," + contact.getEmail2() + ","
					+ contact.getBday() + "," + contact.getBmonth() + "," + contact.getByear() + "," + contact.getGroup() + "," + contact.getAddress2() + ","
					+ contact.getPhone2() + "," + ",!" + "\n");
		}
		writer.close();
	}

	public static List<ContactData> loadContactsFromCsvFile(File file) throws IOException
	{
		List<ContactData> list = new ArrayList<ContactData>();
		FileReader reader = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(reader);
		String line = bufferedReader.readLine();
		while (line != null)
		{
			String[] part = line.split(",");
			ContactData contact = new ContactData()
					.withFirstname(part[0])
					.withLastname(part[1])
					.withAddress(part[2])
					.withPhoneHome(part[3])
					.withPhoneMobile(part[4])
					.withPhoneWork(part[5])
					.withEmail1(part[6])
					.withEmail2(part[7])
					.withBday(part[8])
					.withBmonth(part[9])
					.withByear(part[10])
					.withGroup(part[11])
					.withAddress2(part[12])
					.withPhone2(part[13]);
			list.add(contact);
			line = bufferedReader.readLine();
		}
		bufferedReader.close();
		return list;
	}

	public static List<ContactData> generateRandomContacts(int amount)
	{
		String prefix = "";
		prefix = getPrefix(prefix);

		List<ContactData> list = new ArrayList<ContactData>();
		for (int i = 0; i < amount; i++)
		{
			ContactData contact = new ContactData()
					.withFirstname(generateRandomSuffix(prefix + "firstname "))
					.withLastname(generateRandomSuffix(prefix + "lastname "))
					.withAddress(generateRandomSuffix(prefix + "address "))
					.withPhoneHome(getRandomNumbers(9).toString())
					.withPhoneMobile(getRandomNumbers(9).toString())
					.withPhoneWork(getRandomNumbers(9).toString())
					.withEmail1(generateRandomSuffix("email") + "@mailexample1.com")
					.withEmail2(generateRandomSuffix("email") + "@mailexample2.com");
			if (prefix == "")
			{
				contact.withBday(getRandomNumber(1, 31).toString())
						.withBmonth(getRandomMonth())
						.withByear(getRandomNumber(1900, 2013).toString())
						.withGroup("")
						.withAddress2(generateRandomSuffix("secondary address "))
						.withPhone2(getRandomNumbers(9).toString());
			}
			list.add(contact);
		}

		return list;
	}
}
